﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using LBSWebService.BusinessLogic.BusinessObjects;
using System.Collections.Generic;
using System.Configuration;
using LBSWebService.BusinessLogic;
using System.Web.Security;
using LBSWebService.DataAccess;

namespace LBSWebService
{
    /// <summary>
    /// Summary description for LBSService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LBSService : System.Web.Services.WebService
    {
        [WebMethod]
        public Guid LoginUser(string pUserName, string pPassword)
        {
            try
            {
                if (Membership.ValidateUser(pUserName, pPassword))
                {
                    MembershipUser user = Membership.GetUser(pUserName);
                    if (user.IsLockedOut)
                    {
                        throw new ApplicationException("User is locked out");
                    }
                    return (Guid)user.ProviderUserKey;
                }
                else
                {
                    throw new ApplicationException("Unregistered user!");
                }
            }
            catch (Exception)
            {
                throw new ApplicationException();
            }
        }

        [WebMethod]
        public List<UserProfileObject> GetMatcingUsersNearby(LocationObject pUsersLocation, Guid pUserId)
        {
            try
            {
                LBSDataAccessLayer.UpdateLocationObjectPool(pUsersLocation, pUserId);
               
                                
                List<CurrentLocation> currLocations = DistanceCalculation.GetUsersInClose(pUserId);
                return null;

            //     if (usersCurrentLocations.Count > 0)
            //{
            //    List<UserProfile> prof = usersCurrentLocations.Select<CurrentLocation, UserProfile>(crl => 
            //        new LBSDataAccessLayer.GetUserProfile(crl.UserId))
            //        .ToList()
            //        .Select<UserProfile,UserProfileObject>(up => 
            //            {               
                            
            //               ProfileCommon profile = ProfileCommon.Create(up.UserItem.UserName);
            //               return new UserProfileObject()
            //                 {
                             
            //                 }
            //            });

            }
            catch
            {
               return null;
            }
        }

    }
}
